package mysql

import (
	"gorm.io/gorm"
	"product-user-module/productrpc/productrpc"
)

type Goods struct {
	gorm.Model
	Title     string  `gorm:"type:varchar(50);not null;comment:'商品标题'"`
	GoodPrice float64 `gorm:"type:decimal(10,2);not null;comment:'商品价格'"`
	GoodStock int64   `gorm:"type:int(11);not null;comment:'商品库存'"`
	UserId    int64   `gorm:"type:int(11);not null;comment:'用户ID'"`
}

func (g *Goods) GoodAdd(db *gorm.DB) error {
	return db.Debug().Create(g).Error
}

func ProductList(db *gorm.DB, in *productrpc.ProductListRequest) []*productrpc.ProductList {
	var products []*productrpc.ProductList

	db.Model(&Goods{}).Select(
		"goods.title",
		"goods.good_price",
		"goods.good_stock",
		"goods.user_id",
		"users.account",
	).Joins("left join users on users.id = goods.user_id").Scopes(Paginate(int(in.Page), int(in.Size))).Find(&products)

	return products

}
